package com.example.demo.controller;


import com.example.demo.entity.*;
import com.example.demo.service.impl.ExamExamDetailServiceImpl;
import com.example.demo.service.impl.ExamExamServiceImpl;

import com.example.demo.service.impl.ExamUserServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/exam")
@RequiredArgsConstructor
@Component
public class ExamExamController {
    private final ExamExamServiceImpl examExamService;
    private final ExamExamDetailServiceImpl examExamDetailService;
    private final ExamUserServiceImpl examUserService;
    @PostMapping("/addexam")
    public boolean addExam(@RequestBody ExamExam examExam) {

        boolean bo=examExamService.save(examExam);
        List<ExamUser> list=examUserService.selectGroup(examExam.getGroupid());
        List<ExamExamDetail> list1=new ArrayList<>();
        for (ExamUser examUser : list) {
            ExamExamDetail examDetail = new ExamExamDetail();
            examDetail.setUserid(examUser.getId());
            examDetail.setExamid(examExam.getId());
            examDetail.setIssubmit(2);
            examDetail.setQuestioninfo("");
            list1.add(examDetail);
        }
        examExamDetailService.saveBatch(list1);
        return bo;

    }
    @PostMapping("/deleteexam")
    public void deleteExam(@RequestBody ExamPaper examPaper) {
        String[] idArr=examPaper.getQuestionid().split(",");
        for(int i=0;i< idArr.length;i++){
            examExamService.deleteById(Integer.parseInt(idArr[i]));
        }
    }

    @GetMapping("/selectexam")
    public List<ExamList> selectExam() {
        List<ExamList> list =examExamService.selectExam();
        List<ExamList> list1=new ArrayList<>();
        for (ExamList item:
                list) {
            if(item.getDuration()!=null){
                list1.add(item);
            }
        }
        return list1;
    }

    @GetMapping("/examlist")
    public List<ExamList> examList(Integer userId) {
        ExamUser examUser=examUserService.selectById(userId);
        String[] groupList=examUser.getGroupid().split(",");
        List<ExamList> list=new ArrayList<>();
        for (int i = 0; i < groupList.length; i++) {
            List<ExamList> examList=examExamService.selectByGroup(Integer.parseInt(groupList[i]));
            for (int j = 0; j < examList.size(); j++) {
                ExamExamDetail examDetail= examExamDetailService.selectTakeExam(userId.toString(),examList.get(j).getId());
                if (examDetail==null){
                    list.add(examList.get(j));
                }
            }
        }
        return list;
    }

    @GetMapping("/historylist")
    public List<ExamList> historyList(Integer userId) throws JsonProcessingException {
        List<ExamList> list =examExamService.selectHistoryList(userId.toString());
//        for (int i = 0; i < list.size(); i++) {
//            Integer score=0;
//            JSONObject json = JSONObject.parseObject(list.get(i).getQuestioninfo());
//            String data = json.getString("data");
//            JSONArray jsonArray = JSONArray.parseArray(data);
//            for (int j = 0; j < jsonArray.size(); j++) {
//                JSONObject item = JSONObject.parseObject(jsonArray.get(j).toString());
//                score=score+Integer.parseInt(item.getString("myScore"));
//            }
//            list.get(i).setTotalScore(score);
//        }

        return list;
    }

}
